package com.dzx.design.structural.proxy.staticproxy;

import com.dzx.design.structural.proxy.db.DataSourceContextHolder;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年08月09日 09:49:39
 */
public class OrderServiceStaticProxy {
    private IOrderService iOrderService;

    int saveOrder(Order order) {
        beforeMethod(order);
        iOrderService = new OrderServiceImpl();
        int result = iOrderService.saveOrder(order);
        afterMethod();
        return result;
    }

    private void afterMethod() {
        System.out.println("静态代理after code");
    }

    private void beforeMethod(Order order) {
        System.out.println("静态代理before code");
        Integer userId = order.getUserId();
        int dbRouter = userId % 2;
        System.out.println("静态代理分配到【db" + dbRouter + "】处理数据");
        //设置数据源
        DataSourceContextHolder.setDBType(String.valueOf(dbRouter));
    }
}
